package com.android.sgzcommon.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;

import com.android.sugz.R;

import androidx.appcompat.widget.AppCompatButton;

/**
 * @author sgz
 * @date 2020/12/5
 */
public class SuButton extends AppCompatButton {

    private int mStyle;
    private int mRadius;
    private int mBorderWidth;
    private int mBorderColor;
    private int mStyleTextColor;
    private int mBackgroundColor;
    private ColorStateList mColorStateList;

    private static final int STYLE_PRIMARY = 1;
    private static final int STYLE_WARN = 2;
    private static final int STYLE_DANGER = 3;
    private static final int STYLE_CONFIRM = 4;
    private static final int STYLE_DISABLE = 5;

    private static final int RADIUS_STYLE_DEFAULT = 0;
    private static final int RADIUS_STYLE_CIRCLE = 1;

    public SuButton(Context context) {
        this(context, null);
    }

    public SuButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.buttonStyle);
    }

    public SuButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SuButton);
        mStyle = array.getInt(R.styleable.SuButton_style, 0);
        mColorStateList = array.getColorStateList(R.styleable.SuButton_android_textColor);
        mRadius = array.getDimensionPixelSize(R.styleable.SuButton_radius, getResources().getDimensionPixelSize(R.dimen.btn_radius));
        int radiusStyle = array.getInt(R.styleable.SuButton_radius_style, RADIUS_STYLE_DEFAULT);
        if (RADIUS_STYLE_CIRCLE == radiusStyle) {
            mRadius = Integer.MAX_VALUE;
        }
        mBorderWidth = array.getDimensionPixelSize(R.styleable.SuButton_border_width, getResources().getDimensionPixelSize(R.dimen.btn_border_width));
        boolean stateAnimator = array.getBoolean(R.styleable.SuButton_state_animator, true);
        Drawable backgroundDrawable = array.getDrawable(R.styleable.SuButton_android_background);
        boolean stroke = array.getBoolean(R.styleable.SuButton_stroke, false);
        int styleColor;
        if (STYLE_PRIMARY == mStyle) {
            styleColor = getResources().getColor(R.color.colorPrimary);
        } else if (STYLE_WARN == mStyle) {
            styleColor = getResources().getColor(R.color.btn_warn);
        } else if (STYLE_DANGER == mStyle) {
            styleColor = getResources().getColor(R.color.btn_danger);
        } else if (STYLE_CONFIRM == mStyle) {
            styleColor = getResources().getColor(R.color.btn_confirm);
        } else if (STYLE_DISABLE == mStyle) {
            styleColor = getResources().getColor(R.color.btn_disable);
        } else {
            stroke = true;
            styleColor = getResources().getColor(R.color.btn_normal);
        }
        mBorderColor = array.getColor(R.styleable.SuButton_border_color, styleColor);
        if (stroke) {
            mStyleTextColor = styleColor;
            mBackgroundColor = Color.WHITE;
        } else {
            mStyleTextColor = Color.WHITE;
            mBackgroundColor = styleColor;
        }
        if (mColorStateList == null) {
            mColorStateList = ColorStateList.valueOf(mStyleTextColor);
        }
        if (backgroundDrawable instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) backgroundDrawable;
            mBackgroundColor = colorDrawable.getColor();
        }
        array.recycle();
        if (!stateAnimator) {
            setStateListAnimator(null);
        }
        int textSize = getResources().getDimensionPixelSize(R.dimen.btn_text_size);
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        int padding = getResources().getDimensionPixelSize(R.dimen.btn_padding);
        setPadding(padding, 0, padding, 0);
        setGravity(Gravity.CENTER);
        initStyle(mStyle);
    }

    /**
     *
     */
    private void initStyle(int style) {
        int borderColor = mBorderColor;
        int backgroundColor = mBackgroundColor;
        ColorStateList textColor = mColorStateList;
        if (STYLE_DISABLE == style) {
            backgroundColor = getResources().getColor(R.color.btn_disable);
            borderColor = backgroundColor;
            textColor = ColorStateList.valueOf(getResources().getColor(R.color.grey_500));
        }
        setTextColor(textColor);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(mRadius);
        drawable.setColor(backgroundColor);
        drawable.setStroke(mBorderWidth, borderColor);
        setBackground(drawable);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            initStyle(mStyle);
        } else {
            initStyle(STYLE_DISABLE);
        }
    }
}
